﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using DAL;

namespace _14期Razor引擎
{
    /// <summary>
    /// Edit 的摘要说明
    /// </summary>
    public class Edit : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";
            int id = Convert.ToInt32(context.Request["id"]);
            if (context.Request["title"]!=null)
            {
                //进行修改

                int val= SqlHelper.ExecteNonQueryText("update MyClass set Classname=@name where Classid=@id", new SqlParameter[]
                {
                    new SqlParameter("@name", context.Request["title"].ToString()),
                    new SqlParameter("@id", SqlDbType.Int)
                    {
                        Value = id
                    },
                });
                if (val>0)
                {
                    context.Response.Redirect("listRazor.ashx");
                }
            }
            else
            {
                //进行呈现
                DataTable dt = SqlHelper.ExecuteDataSetText("select * from Myclass where classid=@id ", new SqlParameter("@id", id)).Tables[0];
                ClassModel cm = new ClassModel()
                {
                    Classid = id,
                    ClassName = dt.Rows[0][1].ToString()
                };
                RazorHelper.Render("Edit.html", cm, new RazorHelper.IncludeType()
                {
                    Title = "Top",
                    Content = "Top.html",
                    IsFile = true
                });
            }

            
            

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}